package com.foxhorse.androidpractice.activity.customizeview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.view.View;
import android.widget.LinearLayout;

import com.foxhorse.androidpractice.R;
import com.foxhorse.androidpractice.activity.BaseActivity;

/**
 * Created by fanyang on 2017/8/2.
 * 主要用户绘制一些基础的图形
 */

public class CustomizeDrawView2Activity extends BaseActivity {
    private final static String TAG = "CustomizeDrawView2Activity";
    private LinearLayout ll;
    private Paint paint;
    private MyView myView;
    private Path path;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_customize_view_1);
        ll = (LinearLayout) findViewById(R.id.ll);
//        开启抗锯齿
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        myView = new MyView(this);
        ll.addView(myView);
    }

    class MyView extends View {
        public MyView(Context context) {
            super(context);
        }

        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            //绘制路径
            paint.setStyle(Paint.Style.FILL_AND_STROKE);
            path = new Path(); // 初始化 Path 对象
            {
                // 使用 path 对图形进行描述（这段描述代码不必看懂）
                path.addArc(200, 200, 400, 400, -225, 225);
                path.arcTo(400, 200, 600, 400, -180, 225, false);
                path.lineTo(400, 542);
            }
            canvas.drawPath(path, paint);
//            绘制圆角矩形
            paint.setColor(Color.parseColor("#493311"));
            paint.setStyle(Paint.Style.STROKE);
            canvas.drawRoundRect(50, 600, 400, 900, 50, 50, paint);
            paint.setStyle(Paint.Style.FILL_AND_STROKE);
            canvas.drawRoundRect(500, 600, 800, 900, 50, 50, paint);
            //绘制弧形和扇形
            paint.setColor(Color.parseColor("#cc7711"));
            canvas.drawArc(50, 950, 300, 1200, -90, 90, true, paint);
            paint.setColor(Color.parseColor("#11cc11"));
            canvas.drawArc(350, 950, 600, 1200, 110, 220, false, paint);
            paint.setStyle(Paint.Style.STROKE);
            paint.setColor(Color.parseColor("#9944cc"));
            canvas.drawArc(650, 950, 900, 1200, 110, 220, false, paint);
//path添加圆
            path = new Path();
            path.addCircle(100, 1250, 50, Path.Direction.CW);
            canvas.drawPath(path, paint);
            //path画线
            path = new Path();
            path.lineTo(100, 100);
            path.rLineTo(100, 0);
            canvas.drawPath(path, paint);
            //贝塞尔曲线
            path = new Path();
            path.moveTo(200,900);
            path.quadTo(10, 100, 200, 200);
            path.rQuadTo(100, 300, 250, 444);
            canvas.drawPath(path, paint);
        }
    }
}